package com.zlk.algorithm.algorithm.sort.basesSimp;

import com.zlk.algorithm.algorithm.sort.SortUtils;
import org.junit.Test;

import java.util.Arrays;

/**
 * @program: algorithm
 * @ClassName SelectionSort
 * @description: 选择排序
 * @author: slfang
 * @create: 2023-12-19 11:09
 * @Version 1.0
 **/
public class SelectionSort {

    @Test
    public void test(){
        int testTimes = 100000;
        int randomLength = 20;
        int randomVal = 100;

        for (int i = 0; i < testTimes; i++) {
            int[] randomArr1 = SortUtils.getRandomArr(randomVal, randomLength);
            int[] copy = SortUtils.copyArray(randomArr1);
            SortUtils.comparator(copy);
            sort2(randomArr1);
            if(!SortUtils.isEqual(copy,randomArr1)){
                System.out.println("第"+(i+1)+"测试失败"+ Arrays.asList(copy));
                System.out.println("第"+(i+1)+"测试失败"+ Arrays.asList(randomArr1));
                return ;
            }

        }
        System.out.println("success !!!");
    }



    public void selectionSort(int[] arr){
        if (arr == null || arr.length < 2) {
            return;
        }

        for (int i = 0; i < arr.length-1; i++) {
            int min = i;
            for (int j = i+1; j < arr.length; j++) {
                if(arr[j]<arr[min]){
                    min = j;
                }
            }
            SortUtils.swap(arr,i,min);
        }
    }







    public void sort2(int[] arr){
        if(arr==null||arr.length==0){
            return ;
        }
        for (int i = 0; i < arr.length; i++) {
            int min = i;
            for (int j = i+1; j <arr.length ; j++) {
                if(arr[j]<arr[min]){
                    min = j;
                }
            }
            SortUtils.swap(arr,i,min);
        }

    }




}
